//
//  NGovOnlineViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/23.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NGovOnlineViewController.h"
#import "NGovOnlineCell.h"
#import "NBigDataApplyCommontVC.h"
#import "NGovonlineModel.h"
@interface NGovOnlineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation NGovOnlineViewController

#define cellIdentifier @"gov"
#define SectionHeaderHeight  10.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 192.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NGovOnlineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        //        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"政府在线" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self.view addSubview:self.tableView];
    [self loadData];
    
//    [self LOADVIEW];
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [[NNRCNetRequest manager]getWithUrl:kGovOnlineUrl params:nil success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSArray *arr = result[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NGovonlineModel *model = [NGovonlineModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
    }];
}
#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    // return  self.titleArray.count;
    return self.dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NGovOnlineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NGovonlineModel *model = self.dataArray[indexPath.section];
    cell.model = model;
    
    [cell.dialBtn addTarget:self action:@selector(dial:) forControlEvents:UIControlEventTouchUpInside];
     [cell.gotoNetBtn addTarget:self action:@selector(gotoNet:) forControlEvents:UIControlEventTouchUpInside];
    return cell;

}

//-(void)clickCellButtonMethod:(UIButton *)btn WithEvent:(id)event{
//
//    //取到 button所在的cell的indexPath
//    NSSet *touches =[event allTouches];
//    UITouch *touch =[touches anyObject];
//    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
//    NSIndexPath *indexPath= [self.tableView indexPathForRowAtPoint:currentTouchPosition];
//    if (indexPath != nil){
//        //do something
//
//    }
//
//}
//


- (void)gotoNet:(UIButton *)btn{
    
    UIView *contentView = (UIView *)[btn superview];
    NGovOnlineCell *cell = (NGovOnlineCell *)[contentView superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NGovonlineModel *model = self.dataArray[index.section];
    NBigDataApplyCommontVC *data = [NBigDataApplyCommontVC new];
    //还需要判断是否是正常的网站
    if (![model.governmentOnlineWebsite containsString:@"www."]){
        [AFToast showText:@"请使用合法网址"];
        return;
        
    }else{
        NSString *url;
        if (![model.governmentOnlineWebsite containsString:@"http"]) {
            url = [NSString stringWithFormat:@"http:%@",model.governmentOnlineWebsite];
        }else{
            url = model.governmentOnlineWebsite;
        }
        data.url = url;
        [self.navigationController pushViewController:data animated:YES];
    }
    
}
///
///拨打电话  有两种方法   可用代理和不用代理 实现代理可以实现的拿到索引功能 使用代理的方法可以在雄安通APP，我的预约中进行查看
- (void)dial:(UIButton *)btn{
    
    //无代理获取索引
    UIView *contentView = (UIView *)[btn superview];
    NGovOnlineCell *cell = (NGovOnlineCell *)[contentView superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NGovonlineModel *model = self.dataArray[index.section];
    
    //还需要判断是否是正常的电话
    YYLog(@"当前电话:%@,%ld",model.governmentOnlinePhone,index.section);
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.governmentOnlinePhone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

@end
